﻿/*
* 创建者：LIYINFAI
* 创建时间：2023/10/24 9:35:26
* 描述：
* --------------------------------------
* 修改说明：
* --------------------------------------
*/
using Newtonsoft.Json;
using System;
using System.ComponentModel;
using System.IO;

namespace CommonLibrary
{
    /// <summary>
    /// 其他配置项
    /// </summary>
    public class BusinessConfig
    {
        /// <summary>
        /// 配置文件路径
        /// </summary>
        static string CONFIG_FILE_PATH = Path.Combine(CommonHelper.COMMON_CONFIG_DIR, "BusinessConfig.json");
        /// <summary>
        /// 锁
        /// </summary>
        private static object LOCK_OBJ = new object();
        /// <summary>
        /// 实例对象
        /// </summary>
        private static BusinessConfig instance = null;
        /// <summary>
        /// 实例对象
        /// </summary>
        /// <param name="reload"></param>
        /// <returns></returns>
        public static BusinessConfig Instance(bool reload = false)
        {
            if (reload || instance == null)
            {
                try
                {
                    instance = CommonHelper.GetJsonFromFile(CONFIG_FILE_PATH, new BusinessConfig());
                }
                catch (Exception ex)
                {
                    instance = new BusinessConfig();
                    LogHelper.Error(ex);
                }
            }
            return instance;
        }
        /// <summary>
        /// 保存
        /// </summary>
        public void Save()
        {
            lock (LOCK_OBJ)
            {
                CommonHelper.SaveToJsonFile(instance, CONFIG_FILE_PATH);
            }
        }
        /// <summary>
        /// PING网络的超时时间，默认500ms
        /// </summary>
        [Description("PING网络的超时时间")]
        [JsonProperty(PropertyName = "PingTimeout")]
        public int PingTimeout { get; set; } = 500;
        /// <summary>
        /// 扫码超时时间，默认1000ms
        /// </summary>
        [Description("扫码超时时间")]
        [JsonProperty(PropertyName = "ScanTimeout")]
        public int ScanTimeout { get; set; } = 1000;
        /// <summary>
        /// 扫码重复次数，默认3次
        /// </summary>
        [Description("扫码重复次数")]
        [JsonProperty(PropertyName = "ScanRepeatTimes")]
        public int ScanRepeatTimes { get; set; } = 3;
        /// <summary>
        /// 条码长度，默认40
        /// </summary>
        [Description("条码长度")]
        [JsonProperty(PropertyName = "BarcodeLength")]
        public int BarcodeLength { get; set; } = 40;
        /// <summary>
        /// 是否需要检查条码产长度，默认否
        /// </summary>
        [Description("是否需要检查条码产长度")]
        [JsonProperty(PropertyName = "IsCheckBarcodeLength")]
        public bool IsCheckBarcodeLength { get; set; } = false;
        /// <summary>
        /// 条码前缀，默认空
        /// </summary>
        [Description("条码前缀")]
        [JsonProperty(PropertyName = "BarcodePrev")]
        public string BarcodePrev { get; set; } = "";
        /// <summary>
        /// 是否需要检查条码前缀，默认否
        /// </summary>
        [Description("是否需要检查条码前缀")]
        [JsonProperty(PropertyName = "IsCheckBarcodePrev")]
        public bool IsCheckBarcodePrev { get; set; } = false;
        /// <summary>
        /// ST5520通讯超时时间，默认300ms
        /// </summary>
        [Description("ST5520通讯超时时间")]
        [JsonProperty(PropertyName = "ST5520Timeout")]
        public int ST5520Timeout { get; set; } = 300;
        /// <summary>
        /// H3153特殊延迟时间，避免所有机器同时触发，默认20ms
        /// </summary>
        [Description("H3153特殊延迟时间")]
        [JsonProperty(PropertyName = "H3153SpecialDelay")]
        public int H3153SpecialDelay { get; set; } = 20;
        /// <summary>
        /// H3153通讯超时时间，默认300ms
        /// </summary>
        [Description("H3153通讯超时时间")]
        [JsonProperty(PropertyName = "H3153Timeout")]
        public int H3153Timeout { get; set; } = 300;
        /// <summary>
        /// H3153状态查询间隔，默认300ms
        /// </summary>
        [Description("H3153状态查询间隔")]
        [JsonProperty(PropertyName = "H3153StateInterval")]
        public int H3153StateInterval { get; set; } = 300;
        /// <summary>
        /// H3153设置耐压测试电压命令间隔，默认100ms
        /// </summary>
        [Description("H3153设置耐压测试电压命令间隔")]
        [JsonProperty(PropertyName = "H3153SetWithstandInterval")]
        public int H3153SetWithstandInterval { get; set; } = 100;
        /// <summary>
        /// H3153测试完毕后是否需要额外给两次设置电压的命令，默认是需要
        /// </summary>
        [Description("H3153测试完毕后是否需要额外给两次设置电压的命令")]
        [JsonProperty(PropertyName = "H3153NeedExtraWithstandCommand")]
        public bool H3153NeedExtraWithstandCommand { get; set; } = true;

        /// <summary>
        /// 天平通讯超时时间，默认300ms
        /// </summary>
        [Description("天平通讯超时时间")]
        [JsonProperty(PropertyName = "GagfTimeout")]
        public int GagfTimeout { get; set; } = 300;
        /// <summary>
        /// 天平称重命令是否为Q，默认false
        /// </summary>
        [Description("天平称重命令是否为Q")]
        [JsonProperty(PropertyName = "GagfIsQ")]
        public bool GagfIsQ { get; set; } = false;
        /// <summary>
        /// 当遇到称重为0时，是否进行二次称重，默认 true
        /// </summary>
        [Description("天平二次称重")]
        [JsonProperty(PropertyName = "GagfWeight2")]
        public bool GagfWeight2 { get; set; } = true;
        /// <summary>
        /// 清零返回命令是否包含ACK处理，默认 false
        /// </summary>
        [Description("清零返回命令是否包含ACK处理")]
        [JsonProperty(PropertyName = "GagfRezeroAck")]
        public bool GagfRezeroAck { get; set; } = false;
        /// <summary>
        /// RJ6902通讯超时时间，默认300ms
        /// </summary>
        [Description("RJ6902通讯超时时间")]
        [JsonProperty(PropertyName = "RJ6902Timeout")]
        public int RJ6902Timeout { get; set; } = 300;
        /// <summary>
        /// RJ6902延时查询时间，默认300ms
        /// </summary>
        [Description("RJ6902延时查询时间")]
        [JsonProperty(PropertyName = "RJDelayQueryTimes")]
        public int RJDelayQueryTimes { get; set; } = 300;
        /// <summary>
        /// 氦检仪通讯超时时间，默认300ms
        /// </summary>
        [Description("氦检仪通讯超时时间")]
        [JsonProperty(PropertyName = "HeliumTimeout")]
        public int HeliumTimeout { get; set; } = 300;
        /// <summary>
        /// Modbus通讯超时时间，默认300ms
        /// </summary>
        [Description("Modbus通讯超时时间")]
        [JsonProperty(PropertyName = "ModbusTimeout")]
        public int ModbusTimeout { get; set; } = 300;


        /// <summary>
        /// 历史数据最大查询天数，默认31天
        /// </summary>
        [Description("历史数据最大查询天数")]
        [JsonProperty(PropertyName = "MaxDaysInQueryData")]
        public int MaxDaysInQueryData { get; set; } = 31;


        /// <summary>
        /// 是否显示logo，默认是
        /// </summary>
        [Description("是否显示logo")]
        [JsonProperty(PropertyName = "IsLogoShown")]
        public bool IsLogoShown { get; set; } = true;
        /// <summary>
        /// 是否自动启动，默认否
        /// </summary>
        [Description("是否自动启动")]
        [JsonProperty(PropertyName = "IsAutoStart")]
        public bool IsAutoStart { get; set; } = false;
        /// <summary>
        /// 产品数据是否间隔保存，默认否
        /// </summary>
        [Description("产品数据是否间隔保存")]
        [JsonProperty(PropertyName = "IsProductIntervalSave")]
        public bool IsProductIntervalSave { get; set; } = false;
        /// <summary>
        /// 数据间隔保时间，默认300秒
        /// </summary>
        [Description("数据间隔保时间")]
        [JsonProperty(PropertyName = "ProductIntervalSleepTimes")]
        public int ProductIntervalSleepTimes { get; set; } = 300;
        /// <summary>
        /// 产量清零PLC地址
        /// </summary>
        [Description("产量清零PLC地址")]
        [JsonProperty(PropertyName = "ZeroNumAddress")]
        public string ZeroNumAddress { get; set; } = "";
        /// <summary>
        /// 用户登陆方式，0用户密码，1刷卡
        /// </summary>
        [Description("用户登陆方式")]
        [JsonProperty(PropertyName = "LoginType")]
        public ELoginType LoginType { get; set; } = ELoginType.USERNAME;
        /// <summary>
        /// 用户登陆方式，0用户密码，1刷卡
        /// </summary>
        [Description("MES用户登陆方式")]
        [JsonProperty(PropertyName = "MESLoginType")]
        public ELoginType MESLoginType { get; set; } = ELoginType.USERNAME;
        /// <summary>
        /// 描述信息
        /// </summary>
        /// <returns></returns>
        public override string ToString()
        {
            return CommonHelper.GetObjectString(this);
        }
    }
}
